//
//  RootViewController.m
//  e-nurse
//
//  Created by  on 9/26/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RootViewController.h"
#import "LoginViewController.h"
#import "MainViewController.h"
#import "Database.h"

@interface RootViewController ()

@property (strong, nonatomic) UIAlertView *alertView;

-(void)checkHistoryRecord;
-(void)closeAlertView;

@end

@implementation RootViewController
@synthesize loginViewController = _loginViewController;
@synthesize mainViewController = _mainViewController;
@synthesize ignoreNotifyButton = _ignoreNotifyButton;
@synthesize gotoCheckReportButton = _gotoCheckReportButton;
@synthesize alertView = _alertView;

-(void)dealloc
{
    self.alertView = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addChildViewController:self.mainViewController];
    [self.view addSubview:self.mainViewController.view];
    
    [self checkHistoryRecord];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.loginViewController = nil;
    self.mainViewController = nil;
    self.gotoCheckReportButton = nil;
    self.ignoreNotifyButton = nil;
    self.alertView = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //Hack
    [self.mainViewController viewWillAppear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    [viewController viewWillAppear:NO];
}

- (void)showLoginView
{
    [self.navigationController pushViewController:self.loginViewController animated:YES];
}

- (void)checkHistoryRecord
{
    NSUInteger exceedStdTimes = [[Database sharedInstance] getExceedStdTimesOfCurrentWeek];
    if(exceedStdTimes > 0)
    {
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"NOTIFY_EXCEED_STD_TITLE", nil) message:@"\n\n\n\n\n" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        UILabel *exceedSTDTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,45,260,25)];
        exceedSTDTimesLabel.font = [UIFont systemFontOfSize:16];
        exceedSTDTimesLabel.textColor = [UIColor whiteColor];
        exceedSTDTimesLabel.backgroundColor = [UIColor clearColor];
        exceedSTDTimesLabel.shadowColor = [UIColor blackColor];
        exceedSTDTimesLabel.shadowOffset = CGSizeMake(0,-1);
        exceedSTDTimesLabel.textAlignment = UITextAlignmentCenter;
        exceedSTDTimesLabel.text = [NSString stringWithFormat:NSLocalizedString(@"NOTIFY_EXCEED_STD_7DAY", nil), 0];
        [alertView addSubview:exceedSTDTimesLabel];
        
        UILabel *exceedSTDAccuTimesLabel = [[UILabel alloc] initWithFrame:CGRectMake(12,70,260,25)];
        exceedSTDAccuTimesLabel.font = [UIFont systemFontOfSize:16];
        exceedSTDAccuTimesLabel.textColor = [UIColor whiteColor];
        exceedSTDAccuTimesLabel.backgroundColor = [UIColor clearColor];
        exceedSTDAccuTimesLabel.shadowColor = [UIColor blackColor];
        exceedSTDAccuTimesLabel.shadowOffset = CGSizeMake(0,-1);
        exceedSTDAccuTimesLabel.textAlignment = UITextAlignmentCenter;
        exceedSTDAccuTimesLabel.text = [NSString stringWithFormat:NSLocalizedString(@"NOTIFY_EXCEED_STD_ACCU", nil), 0];
        [alertView addSubview:exceedSTDAccuTimesLabel];
        
        self.gotoCheckReportButton.frame = CGRectMake(55, 120, 175, 37);
        [alertView addSubview:self.gotoCheckReportButton];
        self.ignoreNotifyButton.frame = CGRectMake(55, 160,175, 37);
        [alertView addSubview:self.ignoreNotifyButton];
        
        self.alertView = alertView;
        [alertView show];
        [alertView release];
        [exceedSTDTimesLabel release];
        [exceedSTDAccuTimesLabel release];
    }
}

-(IBAction)ignoreButtonClicked:(id)sender
{
    [self closeAlertView];
}

-(IBAction)gotoCheckReportButtonClicked:(id)sender
{
    [self closeAlertView];
    
    self.mainViewController.selectedIndex = 1;
}

-(void)closeAlertView
{
    [self.alertView dismissWithClickedButtonIndex:0 animated:YES];
    self.alertView = nil;    
}

@end
